<template>
  <div class="login-container">
    <div class="login-wrapper">
      <!-- 左侧背景部分 -->
      <div class="login-bg">
        <div class="bg-content">
          <h2 class="bg-title">欢迎回来</h2>
          <p class="bg-desc">探索更多精彩内容，开启您的专属旅程</p>
          <div class="bg-image"></div>
        </div>
      </div>

      <!-- 右侧表单部分 -->
      <div class="login-form">
        <div class="form-header">
          <div class="logo">
            <i class="el-icon-s-home"></i>
            <span>格智学舟</span>
          </div>
          <div class="toggle-buttons">
            <button 
              class="toggle-btn" 
              :class="{ active: !isRegister }"
              @click="isRegister = false; Clear()">
              登录
            </button>
            <button 
              class="toggle-btn" 
              :class="{ active: isRegister }"
              @click="isRegister = true; Clear()">
              注册
            </button>
          </div>
        </div>

        <!-- 注册表单 -->
        <el-form 
          ref="registerFormRef" 
          size="large" 
          autocomplete="off" 
          v-if="isRegister" 
          :model="registerData" 
          :rules="rules"
          class="auth-form">
          
          <el-form-item prop="username">
            <el-input 
              prefix-icon="el-icon-user" 
              placeholder="用户名/邮箱" 
              v-model="registerData.username">
            </el-input>
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              prefix-icon="el-icon-lock"
              type="password"
              placeholder="请输入密码"
              v-model="registerData.password">
            </el-input>
          </el-form-item>
          
          <el-form-item prop="repsd">
            <el-input
              prefix-icon="el-icon-lock"
              type="password"
              placeholder="请确认密码"
              v-model="registerData.repsd">
            </el-input>
          </el-form-item>
          
          <!-- 注册按钮 -->
          <el-form-item>
            <el-button 
              class="submit-btn" 
              type="primary" 
              size="large"
              @click="Regt">
              立即注册
            </el-button>
          </el-form-item>
        </el-form>
        
        <!-- 登录表单 -->
        <el-form 
          ref="loginFormRef" 
          size="large" 
          autocomplete="off" 
          v-else 
          :model="registerData" 
          :rules="rules"
          class="auth-form">
          
          <el-form-item prop="username">
            <el-input 
              prefix-icon="el-icon-user" 
              placeholder="用户名/邮箱" 
              v-model="registerData.username">
            </el-input>
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              prefix-icon="el-icon-lock"
              type="password"
              placeholder="请输入密码"
              v-model="registerData.password">
            </el-input>
          </el-form-item>
          
          <el-form-item class="remember-me">
            <el-checkbox v-model="rememberMe">记住我</el-checkbox>
            <el-link type="primary" :underline="false">忘记密码？</el-link>
          </el-form-item>
          
          <!-- 登录按钮 -->
          <el-form-item>
            <el-button 
              class="submit-btn" 
              type="primary" 
              size="large"
              @click="Login">
              登录
            </el-button>
          </el-form-item>
        </el-form>
        
        
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';
import { User, Lock } from '@element-plus/icons-vue';
import { Register, UserLoginService } from '@/api/user.js';
import { userTokenStore } from '@/store/userTokenStore.js';

// 表单状态
const isRegister = ref(false);
const rememberMe = ref(false);

const registerData = ref({
  username: '',
  password: '',
  repsd: '',
});

// 密码验证
const validatorForPsd = function(rule, value, callback) {
  if (value === '') {
    callback(new Error('请再次输入密码'));
  } else if (value !== registerData.value.password) {
    callback(new Error('两次输入的密码不一致'));
  } else {
    callback();
  }
};

// 表单验证规则
const rules = reactive({
  username: [
    { required: true, message: '请输入用户名/邮箱', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  repsd: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    { validator: validatorForPsd, trigger: 'blur' }
  ]
});

const router = useRouter();
const tokenStore = userTokenStore();

// 注册处理
const Regt = async ()=>{
   const result = await Register(registerData.value);
   isRegister.value = ! isRegister.value;
   ElMessage.success("注册成功");
}

// 登录处理
const Login = async ()=>{
  const result = await UserLoginService(registerData.value);
  tokenStore.setToken(result.data);
  console.log(result.data);
  await router.push('/mainPage');
}

// 清空表单
const Clear = () => {
  registerData.value = {
    username: '',
    password: '',
    repsd: '',
  };
};

// 表单验证
const validateForm = (formRefName) => {
  return new Promise((resolve, reject) => {
    const formRef = formRefName === 'registerFormRef' ? registerFormRef : loginFormRef;
    formRef.value?.validate((valid) => {
      if (valid) {
        resolve();
      } else {
        reject(new Error('请检查表单信息'));
      }
    });
  });
};
</script>

<style lang="scss" scoped>
/* 整体容器 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4ecf5 100%);
  padding: 20px;
}

.login-wrapper {
  display: flex;
  width: 100%;
  max-width: 1200px;
  min-height: 600px;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  background-color: #fff;
}

/* 背景部分 */
.login-bg {
  flex: 1;
  position: relative;
  background: linear-gradient(135deg, #409EFF 0%, #1867C0 100%);
  overflow: hidden;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.05'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
    opacity: 0.5;
  }
}

.bg-content {
  position: relative;
  z-index: 1;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 60px;
  color: #fff;
}

.bg-title {
  font-size: 36px;
  font-weight: 600;
  margin-bottom: 20px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.bg-desc {
  font-size: 18px;
  line-height: 1.6;
  opacity: 0.9;
  margin-bottom: 40px;
}

.bg-image {
  width: 100%;
  height: 200px;
  background: url('@/assets/logo1.jpg') no-repeat center/cover;
  border-radius: 8px;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
  transform: rotate(2deg);
  transition: transform 0.5s ease;
  
  &:hover {
    transform: rotate(0);
  }
}

/* 表单部分 */
.login-form {
  flex: 1;
  padding: 60px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.form-header {
  margin-bottom: 40px;
  
  .logo {
    display: flex;
    align-items: center;
    margin-bottom: 30px;
    
    i {
      font-size: 32px;
      color: #409EFF;
      margin-right: 10px;
    }
    
    span {
      font-size: 24px;
      font-weight: 600;
      color: #303133;
    }
  }
  
  .toggle-buttons {
    display: flex;
    margin-bottom: 40px;
    
    .toggle-btn {
      flex: 1;
      padding: 12px 0;
      font-size: 18px;
      font-weight: 500;
      background: none;
      border: none;
      border-bottom: 2px solid #EBEEF5;
      color: #606266;
      cursor: pointer;
      transition: all 0.3s ease;
      
      &.active {
        color: #409EFF;
        border-bottom-color: #409EFF;
      }
      
      &:hover:not(.active) {
        color: #909399;
        border-bottom-color: #DCDFE6;
      }
    }
  }
}

.auth-form {
  .el-form-item {
    margin-bottom: 25px;
  }
  
  .el-input {
    height: 50px;
    
    .el-input__inner {
      height: 100%;
      border-radius: 8px;
      padding: 0 16px;
      font-size: 16px;
      border-color: #E4E7ED;
      
      &:focus {
        border-color: #409EFF;
        box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.1);
      }
    }
    
    .el-input__prefix {
      margin-right: 8px;
    }
  }
}

.remember-me {
  display: flex;
  align-items: center;
  margin-bottom: 30px;
  
  .el-checkbox {
    .el-checkbox__label {
      color: #606266;
    }
  }
  
  .el-link {
    font-size: 14px;
    margin-left: auto;
  }
}

.submit-btn {
  height: 50px;
  border-radius: 8px;
  font-size: 18px;
  font-weight: 500;
  transition: all 0.3s ease;
  
  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(64, 158, 255, 0.2);
  }
  
  &:active {
    transform: translateY(0);
  }
}

.social-login {
  margin-top: 40px;
  
  .divider {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
    
    span {
      font-size: 14px;
      color: #909399;
      padding: 0 15px;
    }
    
    &::before,
    &::after {
      content: '';
      flex: 1;
      height: 1px;
      background-color: #EBEEF5;
    }
  }
  
  .social-buttons {
    display: flex;
    justify-content: center;
    
    .social-btn {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      background-color: #F5F7FA;
      display: flex;
      align-items: center;
      justify-content: center;
      margin: 0 10px;
      color: #606266;
      transition: all 0.3s ease;
      border: 1px solid #EBEEF5;
      
      i {
        font-size: 18px;
      }
      
      &:hover {
        background-color: #409EFF;
        color: #fff;
        border-color: #409EFF;
        transform: translateY(-2px);
      }
    }
  }
}

/* 响应式布局 */
@media (max-width: 992px) {
  .login-wrapper {
    flex-direction: column;
    max-width: 500px;
  }
  
  .login-bg {
    display: none;
  }
  
  .login-form {
    padding: 40px;
  }
}
</style>  